﻿@{
    Layout = "~/Areas/Mall/Views/Shared/_HomeLayout.cshtml";
}
<link rel="stylesheet" href="~/Areas/Mall/css/head.css">
<link rel="stylesheet" href="~/Areas/Mall/css/store.css">

<div class="store">
    <div class="location">
        <a href="javascript:history.back(-1);" class="back"><i></i><span>返回上一页</span></a>
        <a href="@Url.Action("Index","Home")"><i></i><span>首页</span></a>-
        <a href="@Url.Action("List","Commodity")"> <span>进货商城</span></a>-
        <span> 商品详情</span>
    </div>
    <div class="detail">
        <input id="txtCommodityId" type="hidden" value="@Model.Id" />
        <img src="@Model.Pic" />
        <div class="content_detail">
            <p class="name"><span>@Model.Name</span></p>
            <p>库存量：<span class="number">@Model.Number</span></p>
            <p>价格：<span class="cost">￥@Model.Money</span></p>
            <p>购买数量：<a class="less">-</a><span class="count" id="spaCount">1</span><a class="add">+</a></p>
            <button onclick="addShoppingCart()"></button>
        </div>
    </div>
</div>
<script type="text/javascript" src="~/Areas/Mall/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    $('.add').on('click', function () {
        let a = $('.count').text();
        let b = parseInt(a);
        b++;
        $('.count').text(b);
    });
    $('.less').on('click', function () {
        let a = $('.count').text();
        let b = parseInt(a);
        if (b > 1) {
            b--;
            $('.count').text(b);
        }
    });

    //添加购物车
    function addShoppingCart() {
        var commodityId = $("#txtCommodityId").val();
        var count = $("#spaCount").text();

        $.ajax({
            type: "POST",
            url: '@Url.Action("AddShoppingCart", "Commodity")',
            data: {
                "CommodityId": commodityId,
                "Number": count
            },
            success: function (res) {
                if (res.Success) {
                    $('.shopping_bag .open').click();
                } else {
                    alert(res.Message)
                }
            }
        });
    }
</script>